﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popup-files.aspx.cs" Inherits="VQWEB.administrator.popup_files" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Popup - Files Manager</title>
    <link href='<%= ResolveUrl("~/uploadify/uploadify.css") %>' rel="stylesheet" type="text/css" />
    <script src='<%= ResolveUrl("~/uploadify/jquery.uploadify.min.js") %>' type="text/javascript"></script>
    <style type="text/css">
        p
        {
            margin: 0px;
        }
    </style>
</head>
<body>
    <div class="modal-dialog" style="width: 800px">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom: 0px">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;</button>
                <form class="form-horizontal" role="form">
                <div class="form-group" style="margin-bottom: 5px">
                    <label class="col-sm-1 control-label">
                        Folder:</label>
                    <div class="col-sm-3">
                        <select class="form-control" id="select_folder">
                        </select>
                    </div>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="folder_name" />
                    </div>
                    <div class="col-sm-2">
                        <button type="submit" id="create_folder" class="btn btn-default">
                            Tạo thư mục</button>
                    </div>
                </div>
                <div class="form-group" style="margin-bottom: 5px">
                    <label class="col-sm-1 control-label">
                        URL:</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="url_files" />
                    </div>
                    <div class="col-sm-2">
                        <button type="submit" id="insert_url" class="btn btn-default">
                            Chèn files
                        </button>
                    </div>
                </div>
                </form>
            </div>
            <div class="modal-body" style="height: 350px; overflow: scroll">
                <div id="show_file" class="container" style="width:750px;">
                    
                </div>
            </div>
            <div class="modal-footer" style="text-align: left; padding-bottom: 5px">
                <div class="row">
                    <div class="col-md-2">
                        <input type="file" name="file_upload" id="file_upload" />
                    </div>
                    <div class="col-md-2">
                        <a href="javascript:Upload()" style="margin-top: 5px; display: inline-block">Upload
                            Files</a>
                    </div>
                </div>
                <div class="row" id="css_progressData">
                </div>
                <div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">

        var url_ashx_upload = "/admin/files/handler";
        var url_swf = '<%=ResolveUrl("~/uploadify/uploadify.swf") %>';

        //Show Files and Folder Page Load
        $(document).ready(function () {
            ShowFolder("uploads");
            ShowFilesByFolder("uploads");
            $("#url_files").val("");
        });

        //Upload Files to Folder
        $(function () {
            $("#file_upload").uploadify({
                'queueID': 'css_progressData',
                'method': 'post',
                'auto': false,
                'fileTypeExts': '*.*',
                'fileSizeLimit': 0,
                'swf': url_swf,
                'uploader': url_ashx_upload,
                'onUploadSuccess': function (file, data, response) {
                    $("#show_file").html(data);
                }
            });
        });

        function Upload() {
            var selectValues = $("#select_folder").val();
            $('#file_upload').uploadify("settings", "formData", { "folder_upload": selectValues });
            $('#file_upload').uploadify('upload', '*');
        }


        //Show Files By Folder
        $("#select_folder").change(function (event) {
            event.preventDefault();
            var f = $("#select_folder").val();
            ShowFilesByFolder(f);
        });


        // Create Folder
        $("#create_folder").click(function (event) {
            event.preventDefault();
            if ($.trim($("#folder_name").val())) {
                var option = $("#select_folder").val();
                var folder_name = $("#folder_name").val();
                var folder_add = option + "/" + folder_name;
                $.post(url_ashx_upload, { folder_add: folder_add }, function (data, xrh) {
                    $("#select_folder").empty();
                    $.each(data, function (index, value) {
                        $("#select_folder").append("<option value=" + value + ">" + value + "</option>");
                        $("#select_folder").val(option);
                    });
                    ShowFilesByFolder(option);
                });
            }
        });

        //Delete File Or Folder
        $('button[type="button"]').live("click", function () {
            var select = $("#select_folder").val();
            var element = $(this).attr("name");
            if (element !== undefined) {
                if (confirm("Chắc chắn muốn xóa?")) {
                    $.post(url_ashx_upload, { deletef: element, select: select }, function (data) {
                        $("#show_file").empty();
                        $("#show_file").html(data);
                        ShowFolder(select);
                    });


                }
                else
                    return false;
            }

        });


        $("#insert_url").click(function (event) {
            event.preventDefault();
            if ($("#url_files").val()) {
                $("#get_files").val("");
                $("#get_files").val($("#url_files").val());
            }

        });

        $(":input[name='urlfiles']").live("click", function () {
            var url = ($(this).val());
            var value = $("#url_files").val();
            if ($(this).prop("checked")) {
                if (value !== "")
                    $("#url_files").val(value + ";" + url);
                else
                    $("#url_files").val(url);
            } else {
                if (value.indexOf(url) == 0)
                    value = value.replace(url, "");
                else
                    value = value.replace(";" + url, "");
                $("#url_files").val(value);
            }
        });


        function ShowFolder(f) {
            $("#select_folder").empty();
            $.get(url_ashx_upload, { show_folder: f }, function (result) {
                $.each(result, function (index, value) {
                    $("#select_folder").append("<option value=" + value + ">" + value + "</option>");
                    $("#select_folder").val(f);
                });
            });
        }

        function ShowFilesByFolder(f) {
            $.get(url_ashx_upload, { show_file: f }, function (data) {
                $("#show_file").empty();
                $("#show_file").html(data);
            });
        }
    </script>
</body>
</html>
